<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of creditAction
 * 用户积分操作
 * @author xlp
 */
class creditAction extends commonAction {

    private $info = array('<span class="red">-</span>', '+');
    private $table = 'event_credit_log'; //数据表
    private $status = array('<span class="red">扣除</span>', '增加');
    private $_type = array( 1 => '<span class="green">佣金积分</span>' , 2 => '<span class="yellow">信用积分</span>');

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    function index() {
        $stime = $this->_get('stime');
        $etime = $this->_get('etime');
        $p = $this->_getid('p', 1);
        $uid = $this->_getid('uid', 0);
        $type = $this->_get('type', 'event');
        $status = $this->_getid('status', 0);
        $isAjax = $this->_getid('is_ajax', 0);
        $where = array('uid' => $uid);
        $userCredit = C('user', 'credit');
        $userCredit = $userCredit['event'];
        $userCredit = array_flip($userCredit);
        if (!$uid) {
            $this->returnJson = false;
            $this->JsonReturn('参数丢失');
        }
        if ($stime && $etime) {
            $stime = inTime($stime);
            $etime = inTime($etime) + (24 * 60 * 60) - 1;
        }
        if ($stime && $etime) {
            $where['infotime[>=]'] = $stime;
            $where['infotime[<=]'] = $etime;
        }
        if ($status) {
            $where['status'] = $status - 1;
        }
        
        $rs = M($this->table)->where($where)->page($p, 10)->order('id DESC')->findAll();
        $total = M($this->table)->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'info' => $this->info, 'status' => $this->status, 'creditType' => $userCredit, 'uid' => $uid, 'type' => $type,'_type' => $this->_type));
        
        if ($isAjax) {
            $this->JsonReturn('ok', $this->getFetch('detail'), 1);
        } else {
            $this->display();
        }
    }

    function add() {
        $uid = $this->_getid('uid');
        if (!$uid) {
            showError('参数丢失');
        }
        
        //检查主题是否存在
        $user = D('member')->field('uid')->where(array('uid' => $uid))->find();
        if (!$user) {
            $this->JsonReturn('用户不存在，请检查');
        }
        $this->assign(array('rs' => $user, 'uid' => $uid));
        $this->display();
    }

    function post() {
        $uid = $this->_postid('uid');
        if (!$uid) {
            showError('参数丢失');
        }
        //检查会员是否存在
        $user = D('member')->field('uid')->where(array('uid' => $uid))->find();
        if (!$user) {
            $this->JsonReturn('用户不存在，请检查');
        }
        $userCredit = C('user', 'credit');
        $objData = array(
            'uid' => $uid,
            'act' => $this->_postid('act', 0),
            'credit' => intval($this->_postid('credit', 0)),
            'event' => $userCredit['event']['admin'], //
            'fid' => admin::$adminInfo['id'],
            'content' => $this->_post('content', '管理员操作'),
            'infotime' => TIME,
        ); 
        $type = $this->_postid('type', 0);
        $actionType = 'credit';
        if ($type == 1) {
            $actionType = 'esteem'; //信用积分
        }
        if ($objData['act']) {
            $objData['act'] = 1;
        }
        if ($objData['credit'] > 10000) {
            $this->JsonReturn('单次增加数量不得超过10000');
        }
        //修改用户积分
        if (D('member')->setUserScore($objData, $actionType)) {
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('积分修改失败');
        }
    }

}
